package com.handong.handserialprogram.module.createmission.recordtask;

import android.support.v7.widget.AppCompatCheckBox;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.handong.handserialprogram.R;
import com.handong.handserialprogram.model.entities.PersonEntity;

import java.util.List;

/**
 * Created by userKey on 2017/8/7.
 */

public class PersonAdapter extends RecyclerView.Adapter<PersonAdapter.CriminalHolder> {

    private CriminalFragment mFragment;
    private List<PersonEntity> mPersonList;
    private int mPersonType;

    public PersonAdapter(CriminalFragment fragment, List<PersonEntity> list, int personType) {
        mFragment = fragment;
        mPersonList = list;
        mPersonType = personType;
    }


    @Override
    public CriminalHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        View rootView = LayoutInflater.from(parent.getContext()).inflate(R.layout.adapter_choose_person, parent, false);
        return new CriminalHolder(rootView);
    }

    @Override
    public void onBindViewHolder(CriminalHolder holder, int position) {
        if (CriminalFragment.TYPE_CRIMINAL == mPersonType) {
            holder.personIcon.setImageResource(R.drawable.icon_criminal_photo);
        } else {
            holder.personIcon.setImageResource(R.drawable.icon_police_photo);
        }

        PersonEntity entity = mPersonList.get(position);
        holder.entity = entity;
        holder.personName.setText(entity.getName());
        holder.personId.setText("编号: " + entity.getId());

        if (entity.isChecked()) {
            holder.personStatus.setChecked(true);
        } else {
            holder.personStatus.setChecked(false);
        }
    }

    @Override
    public int getItemCount() {
        return mPersonList == null ? 0 : mPersonList.size();
    }

    public void onDestroy() {
        mFragment = null;
    }

    class CriminalHolder extends RecyclerView.ViewHolder {

        private ImageView personIcon;
        private TextView personName, personId;
        private AppCompatCheckBox personStatus;
        private PersonEntity entity;

        public CriminalHolder(View itemView) {
            super(itemView);

            personIcon = (ImageView) itemView.findViewById(R.id.choosePersonIcon);
            personName = (TextView) itemView.findViewById(R.id.choosePersonName);
            personId = (TextView) itemView.findViewById(R.id.choosePersonId);
            personStatus = (AppCompatCheckBox) itemView.findViewById(R.id.choosePersonStatus);

            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (!entity.isChecked()) {
                        personStatus.setChecked(true);
                        entity.setChecked(true);
                        mFragment.addCriminal(entity, false);
                    } else {
                        personStatus.setChecked(false);
                        entity.setChecked(false);
                        mFragment.removeCriminal(entity);
                    }
                }
            });
        }
    }
}
